<template>
    <div class='order'>
        <div class="container">
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" class="search-con">
                <!-- <el-col :span="24">
                    <el-form-item>
                        <el-button size="mini" v-for="item, index in btnList" :key="index" @click="changeTab(item, index)"
                            :type="curLine == index ? 'primary' : 'default'">
                            {{ item.dictLabel }}</el-button>
                    </el-form-item>
                </el-col> -->
                <el-form-item label="订单状态" prop="key_orders_state" style="margin-right:25px;">
                    <el-select v-model="queryParams.key_orders_state" placeholder="请选择" clearable size="small">
                        <el-option v-for="dict in btnList" :key="dict.dictValue" :label="dict.dictLabel"
                            :value="dict.dictValue" />
                    </el-select>
                </el-form-item>
                <el-form-item label="订单号：" prop="key_order_no" style="margin-right:25px;">
                    <el-input v-model="queryParams.key_order_no" placeholder="订单编号" clearable size="small" />
                </el-form-item>
                <!-- <el-form-item label="收货人：" prop="key_name" style="margin-right:25px;">
                    <el-input v-model="queryParams.key_name" placeholder="用户名/收货人姓名/手机号码" clearable size="small" />
                </el-form-item> -->
                <el-form-item label="时间范围：" prop="key_range" style="margin-right:25px;">
                    <el-date-picker v-model="queryParams.key_range" type="daterange" range-separator="至"
                        start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" placeholder="请选择下单时间范围" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                        v-hasPermi="['customer:manage:export']">导出
                    </el-button>
                </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5"><span style="font-size:110%;font-weight: bold;color:#30B08F;">共{{ listCount }}个</span></el-col>
                <!-- <el-col :span="22">
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleBulk"
                        v-hasPermi="['customer:manage:add']">
                        批量修改</el-button>
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="title = '创建订单'; handleShow = 'add'"
                        v-hasPermi="['user:depart:add']">
                        创建订单</el-button>
                </el-col> -->
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table border v-loading="loading" :data="tableList" ref="tableList" v-tableHeight
                :height="tableList.length > 0 ? '900px' : '120px'" @selection-change="handleSelectionChange"
                @row-click="toggleSelection">
                <!-- <el-table-column type="selection" width="55" align="center" /> -->
                <el-table-column label="订单编号" align="center" prop="order_no" />
                <el-table-column label="提交时间" align="center" prop="create_time" />
                <el-table-column label="商品总价" align="center" prop="goods_total_price" />
                <el-table-column label="优惠券" align="center" prop="coupon_price" />
                <el-table-column label="运费" align="center" prop="fee_price" />
                <el-table-column label="总商品数量" align="center" prop="total_number" />
                <el-table-column label="已发货数量" align="center" prop="send_goods_number" />
                <el-table-column label="订单总价" align="center" prop="order_total_price" />
                <el-table-column label="支付金额" align="center" prop="pay_price" />
                <el-table-column label="支付状态" align="center" prop="pay_state">
                    <template slot-scope="scope">
                        <span>
                            {{ selectDictLabel(payState, scope.row.pay_state) }}
                        </span>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="支付方式" align="center" prop="pay_type">
                    <template slot-scope="scope">
                        <span>
                            {{ selectDictLabel(payType, scope.row.pay_type) }}
                        </span>
                    </template>
                </el-table-column> -->
                <!-- <el-table-column label="订单来源" align="center" prop="source" /> -->
                <el-table-column label="订单状态" align="center" prop="orders_state">
                    <template slot-scope="scope">
                        <span>
                            {{ selectDictLabel(btnList, scope.row.orders_state) }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-document" @click.stop="toDetail(scope.row)"
                            v-hasPermi="['user:depart:edit']">
                            <!-- @click="title = '订单详情'; form = JSON.parse(JSON.stringify(scope.row)); handleShow = 'detail'" -->
                            查看订单
                        </el-button>
                        <el-button v-if="scope.row.orders_state == 0" icon="el-icon-circle-close" size="mini" type="text"
                            @click.stop="handleCancel(scope.row)" v-hasPermi="['user:depart:start']">取消订单
                        </el-button>
                        <el-button v-if="scope.row.orders_state == 2"
                            @click="orderNo = String(scope.row.order_no); handleShow = 'tracking'" size="mini" type="text"
                            v-hasPermi="['user:depart:stop']">物流跟踪
                        </el-button>

                        <el-button v-if="scope.row.orders_state == 0"
                            @click.stop="orderNo = String(scope.row.order_no); payAmount = String(scope.row.pay_price); handleShow = 'confirm'" size="mini" type="text"
                            v-hasPermi="['user:depart:stop']">去付款
                        </el-button>
                        <el-button v-if="scope.row.orders_state == -1" size="mini" type="text" icon="el-icon-delete"
                            @click.stop="handleDelete(scope.row)" v-hasPermi="['user:depart:del']">
                            删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="pageCount > 0" :pageCount="pageCount" :page.sync="queryParams.page"
                :limit.sync="queryParams.limit" @pagination="getList" />
        </div>
        <add v-if="handleShow == 'add'" :title="title"
            :handleShow="handleShow" @close="curRow = {}; handleShow = ''" @load="getList" />
        <detail v-if="handleShow == 'detail'" :title="title" :handleShow="handleShow" @close="curRow = {}; handleShow = ''"  :data="curRow" @load="getList"></detail>
       <payment v-if="handleShow == 'confirm'" :title="title" :orderNo = "orderNo" :payAmount="payAmount"
            :handleShow="handleShow" @close="handleShow = ''" @load="getList"></payment>
       
            <!-- <confirmPay v-if="handleShow == 'confirm'" :handleShow="handleShow" :orderNo="orderNo" @close="handleShow = ''"
            @load="getList" />
        <deliveySet v-if="handleShow == 'delivery'" :handleShow="handleShow" :orderNo="orderNo" @close="handleShow = ''"
            @load="getList" /> -->
        <!-- <deliveyTrack v-if="handleShow == 'tracking'" :handleShow="handleShow" :orderNo="orderNo" @close="handleShow = ''"
            @load="getList" /> -->
    </div>
</template>
<script>
import { getOrdersLists, getOrdersInfo, canceOrder, delOrder, confirmGoodsPay } from "@/api/purchaseOrder/purchase.js"
import add from './handle/add';
import detail from './handle/detail';
import payment from "./handle/payment"
// import deliveySet from "./handle/deliverySet"
// import deliveyTrack from "./handle/deliveryTrack"
import { departStatus, payType, payState } from "@/api/dictionary"

import throttle from 'lodash.throttle'
export default {
    name: 'order',
    components: {
        add,
        detail,
        payment,
        // confirmPay,
        // deliveySet,
        // deliveyTrack
    },
    props: {

    },
    data() {
        return {
            showSearch: true,
            loading: false,
            pageCount: 0,
            total: 0,
            queryParams: {
                page: 1,
                limit: 20,
                key_order_no: null,
                key_orders_state: '',
                key_range: [],
            },
            curRow: {},
            tableList: [],
            statusList: departStatus,
            payType: payType,
            payState: payState,
            handleShow: "",
            orderNo: null,
            payAmount:'',
            formLoading: false,
            form: {
                id: null,
                name: null,
            },
            rules: {
                name: [{ required: true, message: '请输入部门名称', trigger: 'blur' }],
            },
            title: "",
            size: 'mini',
            curLine: 0,
            btnList: [
                // {
                //     dictLabel: '全部订单',
                //     dictValue: ''
                // },
                {
                    dictLabel: '待付款',
                    dictValue: 0
                },
                {
                    dictLabel: '已付款',
                    dictValue: 1
                },
                {
                    dictLabel: '已取消',
                    dictValue: -1
                }
            ],
            sendStatus: [
                {
                    dictLabel: '已发货',
                    dictValue: '1'
                },
                {
                    dictLabel: '未发货',
                    dictValue: 0
                },
            ],
            listCount:'',
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList() {
            this.loading = true;
            this.queryParams.key_pay_time_start = this.queryParams.key_range.length > 0 ? this.queryParams.key_range[0] : null
            this.queryParams.key_pay_time_end = this.queryParams.key_range.length > 0 ? this.queryParams.key_range[1] : null
            getOrdersLists(this.queryParams).then(res => {
                this.tableList = res.data.lists;
                this.pageCount = res.data.pageCount;
                this.listCount = res.data.listsCount;
                this.loading = false;
            });
        },
        changeTab(item, index) {
            this.curLine = index
            this.queryParams.type = item.dictValue
            this.getList()
        },
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        toggleSelection(row) {
            this.$refs.tableList.toggleRowSelection(row);
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.selectData = selection;
        },
        handleBulk(ids) {
            this.$confirm('是否确认批量修改?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delSector({ id: row.id });
            }).then(() => {
                this.getList();
                this.msgSuccess("修改成功");
            })
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                // return exportBanner(queryParams);
            }).then((response) => {
                // this.download(response.msg);
            });
        },
        close() {
            this.form = {}
            this.handleShow = ""
            this.getList()
        },
        toDetail(row) {
            // this.customerId = row.id
            getOrdersInfo({ order_no: row.order_no }).then(res => {
                this.title = "订单详情"
                this.curRow = res.data;
                this.handleShow = 'detail';
            })
        },
        // handleAble(row) {
        //     // let label = row.status == 0 ? '停用' : '启用';
        //     this.$confirm(`是否将“${row.name}”关闭?`, "警告", {
        //         confirmButtonText: "确定",
        //         cancelButtonText: "取消",
        //         type: "warning"
        //     }).then(function () {
        //         return setSectorStatus({ id: row.id });
        //     }).then(() => {
        //         this.getList();
        //         this.msgSuccess(`${label}成功`);
        //     })
        // },
        handleCancel(row) {
            this.$confirm('是否确认取消' + '"' + row.name + '"' + '?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return canceOrder({ order_no: row.order_no });
            }).then(() => {
                this.getList();
                this.msgSuccess("取消成功");
            })
        },

        handleDelete(row) {
            this.$confirm('是否确认删除' + '"' + row.name + '"' + '?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delOrder({ order_no: row.order_no });
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            })
        },

        confirmPay(row) {
            this.handleShow = 'confirm';
            // getHousingInfo({ code: row.code }).then(res => {
            //     this.title = "商品入库"
            //     this.stockRow = res.data;
            //     this.handleShow = 'audit';
            // })
        },

        submit: throttle(function () {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.formLoading = true;
                    setSector(this.form).then(res => {
                        this.msgSuccess("操作成功！");
                        this.close()
                        this.formLoading = false;
                        this.getList();
                    }).catch(() => {
                        this.formLoading = false;
                    })
                }
            })
        }, 1000, { trailing: false }),
    }
}
</script>

<style lang='scss' scoped>
.decs {

    /deep/.el-descriptions--mini.is-bordered .el-descriptions-item__cell {
        padding: 0 10px;
    }

    /deep/.el-descriptions-item__cell {
        padding-left: 8px;
        font-size: 14px;
    }

    /deep/ .el-col {
        margin: 0px;
        padding: 0px;
        height: 30px;
        line-height: 30px;
    }

    /deep/.el-form-item__error {
        top: 21%;
        right: 5% !important;
        left: unset;
    }
}

.p {
    font-size: 14px;
}
</style>
